package com.xuchaotong.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

/**
 * Created by 357070 on 2017/4/11.
 */
public class SubMain {
    public static final String CHANNEL_NAME = "MyChannel";

    private final static Logger LOGGER = LoggerFactory.getLogger(SubMain.class);

    public static void main(String[] args) throws Exception {
        final Jedis subscriberJedis = RedisClient.getInstance().getJedis();

        final Subscriber subscriber = new Subscriber();
        //订阅线程：接收消息
        new Thread(new Runnable() {
            public void run() {
                try {
                    LOGGER.info("Subscribing to \"MyChannel\". This thread will be blocked.");
                    //使用subscriber订阅CHANNEL_NAME上的消息，这一句之后，线程进入订阅模式，阻塞。
                    subscriberJedis.subscribe(subscriber, CHANNEL_NAME);

                    //当unsubscribe()方法被调用时，才执行以下代码
                    LOGGER.info("Subscription ended.");
                } catch (Exception e) {
                    LOGGER.error("Subscribing failed.", e);
                }
            }
        }).start();

//        subscriberJedis.close();
    }
}
